package demo4.tools;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
//@PropertySource("classpath:config.properties")
public class DruidDBUtil implements IDBUtil{
    @Autowired
    private DruidDataSource dataSource;

//    public DruidDBUtil(){
//        dataSource = new DruidDataSource();
//    }
//    @Value("${spring.datasource.url}")
//    public void setUrl(String url) {
//        dataSource.setUrl(url);
//    }
//    @Value("${spring.datasource.username}")
//    public void setUsername(String username) {
//        dataSource.setUsername(username);
//    }
//    @Value("${spring.datasource.password}")
//    public void setPassword(String password) {
//        dataSource.setPassword(password);
//    }

    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
